﻿
using System;
using System.Threading.Tasks;

namespace Magic.Redis;


/// <summary>
/// 提供Redis客户端操作类（基于CsRedisCore实现）
/// </summary>
public sealed partial class CsRedisCoreClient : IRedisClient
{
	/// <summary>
	/// SetAdd.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public bool SetAdd<T>(string key, T value)
	{
		try
		{
			RedisHelper.SAdd(key, value);
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SetAdd.key={key}&value={Serialize(value)}", ex);
			return default;
		}
	}

	/// <summary>
	/// SetAddAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public async Task<bool> SetAddAsync<T>(string key, T value)
	{
		try
		{
			await RedisHelper.SAddAsync(key, value);
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SetAddAsync.key={key}&value={Serialize(value)}", ex);
			return default;
		}
	}

	/// <summary>
	/// SetPop.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public T SetPop<T>(string key)
	{
		try
		{
			return RedisHelper.SPop<T>(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SetPop.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// SetPopAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <returns></returns>
	public Task<T> SetPopAsync<T>(string key)
	{
		try
		{
			return RedisHelper.SPopAsync<T>(key);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SetPopAsync.key={key}", ex);
			return default;
		}
	}

	/// <summary>
	/// SetRemove.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public bool SetRemove<T>(string key, T field)
	{
		try
		{
			RedisHelper.SRem(key, field);
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SetRemove.key={key}&field={Serialize(field)}", ex);
			return default;
		}
	}

	/// <summary>
	/// SetRemoveAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public async Task<bool> SetRemoveAsync<T>(string key, T field)
	{
		try
		{
			await RedisHelper.SRemAsync(key, field);
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on SetRemoveAsync.key={key}&field={Serialize(field)}", ex);
			return default;
		}
	}
}
